package com.wlsk.license.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.Constructor;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.BiConsumer;

@Slf4j
public class DataUtil {


    public static <T, E> E copyTo(T source, Class<E> destinationClass) {
        try {
            if (source == null) {
                return null;
            }
            E e = destinationClass.newInstance();
            BeanUtils.copyProperties(source, e);
            return e;
        } catch (Exception e) {
            log.error("[copyTo] error:", e);
            return null;
        }
    }

    /**
     * 复制集合
     *
     * @param <E>
     * @param source
     * @param destinationClass
     * @return
     */
    public static <E> List<E> copyTo(List<?> source, Class<E> destinationClass) {
        if (source == null) {
            return null;
        }
        if (source.size() == 0) {
            return Collections.emptyList();
        }
        List<E> res = new ArrayList<E>(source.size());
        try {
            for (Object o : source) {
                E e = destinationClass.newInstance();
                BeanUtils.copyProperties(o, e);
                res.add(e);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    public static <R, E> List<E> copyTo(List<?> source, Class<R> outClass, Class<E> innerClass, R r) {
        if (source.size() == 0) {
            return Collections.emptyList();
        }
        List<E> res = new ArrayList<E>(source.size());
        try {
            for (Object o : source) {
                Constructor<E> declaredConstructor = innerClass.getDeclaredConstructor(outClass);
                declaredConstructor.setAccessible(true);
                E e = declaredConstructor.newInstance(r);
                BeanUtils.copyProperties(o, e);
                res.add(e);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    public static <E> List<E> copyToCallMethod(List<?> source, Class<E> destinationClass, BiConsumer function) {
        if (source.size() == 0)
            return Collections.emptyList();
        List<E> res = new ArrayList<E>(source.size());
        try {
            for (Object o : source) {
                E e = destinationClass.getDeclaredConstructor().newInstance();
                BeanUtils.copyProperties(o, e);
                function.accept(o, e);
                res.add(e);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }


    public static String idHandle(String id) {
        if (StringUtils.isBlank(id)) {
            return id;
        } else if (id.length() <= 9) {
            return id.replaceAll("(?<=[\\w|\\W]{2})[\\w|\\W](?=[\\w|\\W]{2})", "*");
        }
        return id.replaceAll("(?<=[\\w|\\W]{6})[\\w|\\W](?=[\\w|\\W]{2})", "*");
    }

    public static String mobileEncrypt(String mobile) {
        if (StringUtils.isEmpty(mobile) || (mobile.length() != 11)) {
            return mobile;
        }
        return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }

    public static String[] listToStrArray(List source) {
        if (source == null || source.size() == 0) {
            return null;
        }
        String[] values = new String[source.size()];
        for (int i = 0; i < source.size(); i++) {
            values[i] = source.get(i).toString();
        }
        return values;
    }


    public static String replaceNull(String str) {
        if (str == null) {
            return null;
        }
        return str.replace("null", "");
    }

    public static BigDecimal obj2BigDecimal(Object object, BigDecimal defalt) {
        if (Objects.isNull(object)) {
            return defalt;
        }
        try {
            return new BigDecimal(object.toString());
        } catch (NumberFormatException e) {
            return defalt;
        }
    }

    public static String getRandom() {
        Random rand = new Random();//生成随机数
        StringBuffer numer = new StringBuffer();
        for (int a = 0; a < 8; a++) {
            numer.append(rand.nextInt(10));
        }
        return numer.toString();
    }

    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }


    public static String getMerchantCode(long num) {
        return String.format("%03d", num);
    }

    public static int strToInt(String val) {
        int result = 0;
        if(StringUtils.isNotEmpty(val)) {
            try {
                result = Integer.parseInt(val.trim());
            } catch (Exception e) {
                log.error("字符串转数字异常:{}", val, e);
            }
        }
        return result;
    }

    public static long strToLong(String val) {
        long result = 0l;
        if(StringUtils.isNotEmpty(val)) {
            try {
                result = Long.parseLong(val.trim());
            } catch (Exception e) {
                log.error("字符串转数字异常:{}", val, e);
            }
        }
        return result;
    }

    public static Double strToDouble(String val) {
        Double result = 0.0;
        if(StringUtils.isNotEmpty(val)) {
            try {
                result = Double.parseDouble(val.trim());
            } catch (Exception e) {
                log.error("字符串转数字异常:{}", val, e);
            }
        }
        return result;
    }

    public static String nullStringHandle(String val) {
        if(StringUtils.isNotEmpty(val)) {
            return val;
        }
        return "";
    }
}
